package me.lc.superDown.sprite.tools;

import me.lc.superDown.core.GameManager;
import me.lc.superDown.core.GameSystem;
import me.lc.superDown.sprite.GameTool;
import me.lc.superDown.util.AudioHelper;

import com.wiyun.engine.actions.Action.Callback;
import com.wiyun.engine.actions.Animate;
import com.wiyun.engine.actions.MoveTo;
import com.wiyun.engine.actions.RepeatForever;
import com.wiyun.engine.actions.ScaleTo;
import com.wiyun.engine.actions.Spawn;
import com.wiyun.engine.nodes.Animation;
import com.wiyun.engine.nodes.Sprite;
import com.wiyun.engine.types.WYRect;
import com.wiyun.engine.utils.ResolutionIndependent;

public class CoinTool extends GameTool {

	static float widthHeight = ResolutionIndependent.resolveDp(22);

	@Override
	protected void createToolSprite() {
		toolSprite = Sprite.make(GameSystem.tex_coin, frameAt(0));
		Animation animation = new Animation(0);
		animation.addFrame(0.1f, frameAt(0), frameAt(1), frameAt(2),
				frameAt(3), frameAt(4));
		RepeatForever forever = RepeatForever.make(Animate.make(animation));
		forever.autoRelease();
		toolSprite.runAction(forever);
	}

	private WYRect frameAt(int i) {
		return WYRect.make(widthHeight * i, 0, widthHeight, widthHeight);
	}

	@Override
	public float getToolWidth() {
		return widthHeight;
	}

	@Override
	public float getToolHeight() {
		return widthHeight;
	}

	@Override
	public synchronized void hitTool() {
		if (!isHited) {
			AudioHelper.getCoin();
			isHited = true;

			MoveTo move = (MoveTo) MoveTo.make(0.3f, 0, 0, 0,
					ResolutionIndependent.resolveDp(40)).autoRelease();
			ScaleTo scale = (ScaleTo) ScaleTo.make(0.3f, 1.5f, 0.6f)
					.autoRelease();
			Spawn spawn = (Spawn) Spawn.make(move, scale).autoRelease();
			spawn.setCallback(new Callback() {

				@Override
				public void onUpdate(int arg0, float arg1) {

				}

				@Override
				public void onStop(int arg0) {
					toolSprite.setVisible(false);
				}

				@Override
				public void onStart(int arg0) {

				}
			});
			toolSprite.runAction(spawn);

			Thread t = new Thread() {
				@Override
				public void run() {
					GameManager.instance.addCoin();
				}
			};
			t.start();

		}
	}
}
